package cn.tedu.weibo.controller;

import cn.tedu.weibo.common.response.JsonResult;
import cn.tedu.weibo.common.response.StatusCode;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.service.IWeiboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Size;
import java.util.List;

@Api(tags = "微博模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {

    @Autowired
    private IWeiboService weiboService;

    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("发布微博：{}", weiboDTO);
        UserVO user = (UserVO) session.getAttribute("user");
        int res = weiboService.insert(weiboDTO,user.getId());
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation("微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> list = weiboService.selectIndex();
        if(list==null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return new JsonResult(StatusCode.OPERATION_SUCCESS,list);
    }

    @ApiOperation("微博详情")
    @GetMapping("selectById")
    public JsonResult selectById(@Range(min = 1,max = 1000,message = "微博id范围1-1000") @RequestParam Long id,
                                 @Size(min = 1,max = 1000,message = "用户名username范围1-1000") @RequestParam String username
                                ){
         log.debug(id + username);

        WeiboDetailVO weiboDetailVO = weiboService.selectById(id , username);
         if(weiboDetailVO==null){
             return new JsonResult(StatusCode.OPERATION_FAILED);
         }
         return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboDetailVO);
    }
}
